<?php

class admin extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('admin_model');
        if(($this->session->userdata('user_id')== null)){
            redirect(base_url());
        }else if ($this->session->userdata('type')!='administrator'){
            redirect(base_url());
        }
    }

    public function dashboard($tab = '') {
        $id = $this->session->userdata('user_id');
        if (!empty($tab)) {
            if ($tab == 'deactivatestud' || $tab == 'reactivatestud' || $tab == 'resetstud' || $tab == 'editStud' || $tab == 'addStud') {
                $data['activeMenu'] = 'students';
                $data['activeMenuTab'] = 'activeStudents';
                if ($tab == 'deactivatestud') {
                    $data['alert'] = 'Successfully deactivated student.';
                } else if ($tab == 'reactivatestud') {
                    $data['alert'] = 'Successfully reactivated student.';
                } else if ($tab == 'resetstudent') {
                    $data['alert'] = 'Password reset was successful.';
                } else if ($tab == 'editStud') {
                    $data['alert'] = 'Successfully updated student.';
                } else if ($tab == 'addStud') {
                    $data['alert'] = 'Successfully added student.';
                }
            } else if ($tab == 'changepass' || $tab == 'profileupdate' || $tab == 'errpass') {
                $data['activeMenu'] = 'myProfile';
                if ($tab == 'changepass') {
                    $data['alert'] = 'Successfully updated password.';
                } else if ($tab == 'profileupdate') {
                    $data['alert'] = 'Successfully updated profile.';
                } else if ($tab == 'errpass') {
                    $data['error'] = 'Old password entered is invalid.';
                }
            }else if ($tab == 'addsec' ||  $tab == 'editsec' || $tab == 'deactivatesect' || $tab == 'reactivatesect'){
                $data['activeMenu'] = 'sections';
                $data['activeMenuTab'] = 'activeSections';
                 if ($tab == 'addsec') {
                    $data['alert'] = 'Successfully added section.';
                } else if($tab == 'deactivatesect') {
                    $data['alert'] = 'Successfully deactivated section.';
                }else if ($tab == 'reactivatesect'){
                    $data['alert'] = 'Successfully reactivated section.';
                }else if ($tab == 'editsec'){
                    $data['alert'] = 'Successfully updated section.';
                }
            }else if ($tab == 'deactivatemod' || $tab == 'reactivatemod' || $tab == 'resetmoderator' || $tab == 'editmod' || $tab == 'addmod') {
                $data['activeMenu'] = 'moderators';
                $data['activeMenuTab'] = 'activeModerators';
                if ($tab == 'deactivatemod') {
                    $data['alert'] = 'Successfully deactivated moderator.';
                } else if ($tab == 'reactivatemod') {
                    $data['alert'] = 'Successfully reactivated moderator.';
                } else if ($tab == 'resetmoderator') {
                    $data['alert'] = 'Password reset was successful.';
                } else if ($tab == 'editmod') {
                    $data['alert'] = 'Successfully updated moderator.';
                } else if ($tab == 'addmod') {
                    $data['alert'] = 'Successfully added moderator.';
                }
            }
        }
        $data['ActiveStudents'] = $this->admin_model->getStudents('N');
        $data['inActiveStudents'] = $this->admin_model->getStudents('Y');
        $data['title'] = "Admin's Dashboard";
        $data['activeSections'] = $this->admin_model->getStudentCountperSection('N');
        $data['inactiveSections'] = $this->admin_model->getStudentCountperSection('Y');
        $data['activeModerators'] = $this->admin_model->getModerators('N');
        $data['inactiveModerators'] = $this->admin_model->getModerators('Y');
        $this->load->view('templates/header', $data);
        $this->load->view('admin', $data);
        $this->load->view('templates/footer', $data);
    }

    public function editStudent($studid) {
        $data['student_profile'] = $this->admin_model->getStudent($studid);
        $data['sections'] = $this->admin_model->getSections('N');
        $data['title'] = 'Edit Student';
        $data['cancel_link'] = base_url('/admin/dashboard/');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('middlename', 'Middle Name', 'trim');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('section', 'Section', 'required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('EditStudent', $data);
            $this->load->view('templates/footer');
        } else {
            $firstname = ucwords(strtolower($this->input->post('firstname')));
             $middlename = ucwords(strtolower($this->input->post('middlename')));
             $lastname = ucwords(strtolower($this->input->post('lastname')));
            $studentdata = array('firstname' => $firstname, 'middlename' =>$middlename, 'lastname' => $lastname, 'section_id' => $this->input->post('section'));
            $this->admin_model->updateStudent($studid, $studentdata);
            redirect(base_url('/admin/dashboard/editStud/'));
        }
    }

    public function addStudent() {
        $data['sections'] = $this->admin_model->getSections('N');
        $data['title'] = 'Add Student';
        $data['cancel_link'] = base_url('/admin/dashboard/');
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('middlename', 'Middle Name', 'trim');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('section', 'Section', 'required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('AddStudent', $data);
            $this->load->view('templates/footer');
        } else {
            $firstname = ucwords(strtolower($this->input->post('firstname')));
             $middlename = ucwords(strtolower($this->input->post('middlename')));
             $lastname = ucwords(strtolower($this->input->post('lastname')));
            $username = strtolower(substr($this->input->post('firstname'), 0, 1) . $this->input->post('lastname') . rand(1, 500));
            $userdata = array('username' => $username, 'type' => 'student');
            $studentID = $this->admin_model->addUser($userdata);
            $studentdata = array('user_id' => $studentID, 'firstname' => $firstname, 'middlename' => $middlename, 'lastname' =>  $lastname, 'section_id' => $this->input->post('section'));
            $this->admin_model->addStudent($studentdata);
            $this->addStatus($studentID);
            redirect(base_url('/admin/dashboard/addStud/'));
        }
    }
    
    public function editModerator($moderatorid) {
        $data['moderator_profile'] = $this->admin_model->getModerator($moderatorid);
        $data['title'] = 'Edit Moderator';
        $data['cancel_link'] = base_url('/admin/dashboard/');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('middlename', 'Middle Name', 'trim');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('EditModerator', $data);
            $this->load->view('templates/footer');
        } else {
            $firstname = ucwords(strtolower($this->input->post('firstname')));
             $middlename = ucwords(strtolower($this->input->post('middlename')));
             $lastname = ucwords(strtolower($this->input->post('lastname')));
            $moderatordata = array('firstname' => $firstname, 'middlename' => $middlename, 'lastname' => $lastname);
            $this->admin_model->updateModerator($moderatorid, $moderatordata);
            redirect(base_url('/admin/dashboard/editmod/'));
        }
    }
    
    public function addModerator() {
        $data['title'] = 'Add Moderator';
        $data['cancel_link'] = base_url('/admin/dashboard/');
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('middlename', 'Middle Name', 'trim');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('AddModerator', $data);
            $this->load->view('templates/footer');
        } else {
            $firstname = ucwords(strtolower($this->input->post('firstname')));
             $middlename = ucwords(strtolower($this->input->post('middlename')));
             $lastname = ucwords(strtolower($this->input->post('lastname')));
            $username = strtolower(substr($this->input->post('firstname'), 0, 1) . $this->input->post('lastname') . rand(1, 500));
            $userdata = array('username' => $username, 'type' => 'moderator');
            $moderatorID = $this->admin_model->addUser($userdata);
            $moderatorData = array('user_id' => $moderatorID, 'firstname' => $firstname, 'middlename' => $middlename, 'lastname' => $lastname);
            $this->admin_model->addModerator($moderatorData);
            redirect(base_url('/admin/dashboard/addmod/'));
        }
    }
    
    
    public function updateSection($section_id){
        $sectionName = trim($this->input->post('secName'));
        $moderatorID = $this->input->post('moderatorName');
        $data = array('section_name' =>$sectionName,'user_id'=>$moderatorID);
        $this->admin_model->updateSection($section_id,$data);
        redirect(base_url('/admin/dashboard/editsec/'));
    }

    public function deactivateStudent($userid) {
        $userdata = array('isDeleted' => 'Y');
        $this->admin_model->updateUser($userid, $userdata);
        redirect(base_url('/admin/dashboard/deactivatestud'));
    }
    
    public function reactivateModerator($userid) {
        $userdata = array('isDeleted' => 'N');
        $this->admin_model->updateUser($userid, $userdata);
        redirect(base_url('/admin/dashboard/reactivatemod'));
    }
    
    public function deactivateModerator($userid) {
        $userdata = array('isDeleted' => 'Y');
        $this->admin_model->updateUser($userid, $userdata);
        redirect(base_url('/admin/dashboard/deactivatemod'));
    }
    
    public function deactivateSection($section_id) {
        $data = array('isDeactivated' => 'Y');
        $this->admin_model->updateSection($section_id, $data);
        redirect(base_url('/admin/dashboard/deactivatesect'));
    }
    
    public function reactivateSection($section_id) {
        $data = array('isDeactivated' => 'N');
        $this->admin_model->updateSection($section_id, $data);
        redirect(base_url('/admin/dashboard/reactivatesect'));
    }
    
    

    public function changePassword($userid) {
        $moderator_profile = $this->admin_model->getModerator($userid);
        $password = trim($moderator_profile['password']);
        $oldpass = trim($this->input->post('currentPass'));
        $newpass = trim($this->input->post('newPass'));
        if ($password == $oldpass) {
            $userdata = array('password' => $newpass);
            $this->admin_model->updateUser($userid, $userdata);
            redirect(base_url('/moderator/dashboard/changepass'));
        } else {
            redirect(base_url('/moderator/dashboard/errpass'));
        }
    }
    
    public function addSection(){
        $sectionName = trim($this->input->post('sectionName'));
        $moderator = $this->input->post('moderatorName');
        $data = array('user_id' => $moderator,'section_name'=>$sectionName);
        $this->admin_model->addSection($data);
        redirect(base_url('/admin/dashboard/addsec'));
    }

    public function reactivateStudent($userid) {
        $userdata = array('isDeleted' => 'N');
        $this->admin_model->updateUser($userid, $userdata);
        redirect(base_url('/admin/dashboard/reactivatestud'));
    }

    public function resetPassword($userid,$type) {
        $userdata = array('password' => '123');
        $this->admin_model->updateUser($userid, $userdata);
        redirect(base_url('/admin/dashboard/reset'.$type.'/'));
    }

    public function addStatus($userid) {
        $counter = 1;
        while ($counter < 10) {
            $this->admin_model->addStatus(array('user_id' => $userid, 'criteria_id' => $counter, 'doesExist' => 'Y', 'state' => 'B'));
            $counter +=1;
        }
    }

}

?>
